<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="title"><b>玫瑰图库</b></div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          style="width: 100%; height: 100%"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="400px" style="padding-right: 10px">
        <div class="user-login-status" style="text-align: right">
          <div v-if="loginUserStore.loginUser.id">
            欢迎您：&nbsp;<b>{{ loginUserStore.loginUser.userName ?? '无名' }}</b
            >&nbsp;&nbsp;&nbsp;&nbsp;
            <a-dropdown>
              <a class="ant-dropdown-link" @click.prevent>
                <a-avatar :size="40" :src="loginUserStore.loginUser.userAvatar">
                  <template #icon><UserOutlined /></template>
                </a-avatar>
              </a>
              <template v-slot:overlay>
                <a-menu>
                  <a-menu-item>
                    <a href="/user/updateInfo">个人信息</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a href="/user/updatePwd">修改密码</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a @click="userLoginOut">退出登录</a>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
import { computed, h } from 'vue'
import { HomeOutlined } from '@ant-design/icons-vue'
import { useLoginUserStore } from '@/stores/user'
import router from '@/router'
import { ref } from 'vue'
import { type MenuProps, message } from 'ant-design-vue'
import { userLogout } from '@/api/userController.ts'
import PictureManagePage from '@/pages/admin/PictureManagePage.vue'

const loginUserStore = useLoginUserStore()
// 当前高亮的菜单项
const current = ref<string[]>([])
// 监听路由变化，更新当前选中菜单
router.afterEach((to) => {
  current.value = [to.path]
})

const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}

/**
 * 用户退出登录
 */
const userLoginOut = async () => {
  const res = await userLogout()
  if (res.data.code === 0) {
    message.success('退出成功')
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    setTimeout(() => {
      window.location.href = `/user/login?redirect=${window.location.href}`
    }, 500)
  } else {
    message.error('退出失败')
  }
}

// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))

/**
 * 路由列表
 */
// 菜单列表
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/picture/add',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: 'others',
    label: h('a', { href: 'https://www.codefather.cn', target: '_blank' }, '编程导航'),
    title: '编程导航',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu: any) => {
    if (menu.key.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}
</script>

<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
  left: 10px;
}

.title {
  color: black;
  font-size: 16px;
  margin-left: 20px;
}

.logo {
  height: 50px;
  margin-left: 20px;
}
.user-login-status {
  text-align: center;
}
</style>
